package com.hongawen.practise;

import com.hongawen.pojo.Staff;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2021年09月06日 15:53
 */
public class StreamPractise {

    private static List<Staff> staffs;

    private static void initData() {
        staffs = Arrays.asList(new Staff("小花", 8000, 28, "female", "徐州"), new Staff("小洪", 5000, 31, "male", "泰州"),
                new Staff("小强", 5500, 22, "male", "徐州"), new Staff("小美", 6000, 28, "female", "苏州"),
                new Staff("小华", 9000, 28, "male", "泰州"), new Staff("小丽", 4000, 26, "female", "扬州"),
                new Staff("小同", 10000, 25, "male", "南京"), new Staff("小伦", 4500, 22, "male", "南京"),
                new Staff("小黄", 5000, 31, "male", "扬州"), new Staff("小德", 7000, 33, "male", "南京"),
                new Staff("小肥", 9900, 31, "female", "泰州"), new Staff("小雪", 12000, 22, "female", "泰州"));

    }


    public static void main(String[] args) {
        initData();
        List<Staff> moreThan8000 = staffs.stream().filter(staff -> staff.getSalary() >= 8000).collect(Collectors.toList());
        long moreThan8000Numbers = staffs.stream().filter(staff -> staff.getSalary() >= 8000).count();
        Double averageSalaryNumbers = staffs.stream().collect(Collectors.averagingInt(Staff::getSalary));
        System.out.println(averageSalaryNumbers);

        Optional<Integer> reduce = staffs.stream().map(Staff::getSalary).reduce((x, y) -> x + y);
        System.out.println(reduce.get());

        System.out.println(moreThan8000Numbers);
        System.out.println(moreThan8000);
        Staff staff = staffs.stream().max((o1, o2) -> {
            if (o1.getSalary() >= o2.getSalary()) {
                return 1;
            } else {
                return -1;
            }
        }).get();
        System.out.println(staff);

        List<Staff> orderBySalary = staffs.stream().sorted((o1,o2)->{
            if (o1.getSalary() >= o2.getSalary()) {
                return 1;
            } else {
                return -1;
            }
        }).collect(Collectors.toList());
        System.out.println(orderBySalary);


        Map<String, Map<String, List<Staff>>> collect = staffs.stream().collect(Collectors.groupingBy(Staff::getSexy, Collectors.groupingBy(Staff::getArea)));
        System.out.println(collect
        );

    }


}
